package com.briup.server;

import com.briup.entity.Environment;
import com.briup.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Collection;

/**
 * @Classname DBStoreImplTest
 * @Description TODO
 * @Date 2023/11/26 18:39
 * @Created by 崔宗祺
 * @Version v1.0.0
 */
public class DBStoreImplTest implements DBStore {
    @Override
    public void saveDB(Collection<Environment> collection) throws Exception {
        PreparedStatement preparedStatement = null;
        Connection connection = JDBCUtil.getConnection();
        String sql = null;
        for (Environment environment : collection) {
            String gatherDate = String.valueOf(environment.getGatherDate());
            String[] split1 = gatherDate.split("[ ]");
            String[] split2 = split1[0].split("[-]");
            int data = Integer.parseInt(split2[1]);
            sql = "insert into e_detail_"+data+"(name,srcId,desId,devId,count,cmd,data,status,gatherDate) values (?,?,?,?,?,?,?,?,?)";
            preparedStatement= connection.prepareStatement(sql);
            preparedStatement.setString(1,environment.getName());
            preparedStatement.setString(2,environment.getSrcId());
            preparedStatement.setString(3,environment.getDesId());
            preparedStatement.setString(4,environment.getDevId());
            preparedStatement.setInt(5,environment.getCount());
            preparedStatement.setString(6,environment.getCmd());
            preparedStatement.setFloat(7,environment.getData());
            preparedStatement.setInt(8,environment.getStatus());
            preparedStatement.setTimestamp(9,environment.getGatherDate());
            int i = preparedStatement.executeUpdate();
            System.out.println(i);
        }
    }
}
